using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Components.Tabs;
using Framework.Library.Utilities;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Tabs
{
	public partial class TabManagement : AuthenticatedControl
	{
        private ArrayList lstFile = new ArrayList();

		protected void Page_Load(object sender, EventArgs e)
		{
            if (!Page.IsPostBack)
            {
                btnDelete.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
                string cmd = ConvertUtility.ToString(Request.QueryString["cmd"]);
                if (cmd != null && cmd != "")
                {
                    CommandInfo info = CommandController.GetCommand(cmd);
                    lblCommandName.Text = info.CommandName;
                }
            }
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

            GetFiles(Server.MapPath("/"), Server.MapPath("/Layouts"));
            dropPath.DataSource = lstFile;
            dropPath.DataBind();

			LoadTab();
		}
        private void GetFiles(string rootPath, string path)
        {
            string[] files = Directory.GetFiles(path);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].EndsWith(".ascx"))
                {
                    string temp = files[i].Replace(rootPath, string.Empty);
                    temp = temp.Replace("\\", "/");
                    if (!temp.StartsWith("/")) temp = "/" + temp;
                    lstFile.Add(temp);
                }
            }
            string[] folders = Directory.GetDirectories(path);
            for (int i = 0; i < folders.Length; i++)
            {                
                GetFiles(rootPath, folders[i]);
            }
            lstFile.Sort();
        }
		private void LoadTab()
		{
			int curTabId = ConvertUtility.ToInt32(txtID.Text);
			DataTable dtTabs = TabController.GetTabsByPortalID(ConvertUtility.ToInt32(AppEnv.PortalId()));
			tvwCmds.Nodes.Clear();
			DataRow[] drRoots = dtTabs.Select("TabParentID = 0");
			foreach (DataRow row in drRoots)
			{
                TreeNode node = new TreeNode();
				node.Text = row["TabName"].ToString();
				node.Value = row["TabID"].ToString();
                node.Expanded = false;
				tvwCmds.Nodes.Add(node);
				LoadForCurNode(node, dtTabs);
			}

			dropParent.Items.Clear();
			TabController.FillToList(dropParent.Items, dtTabs);
			dropParent.Items.Insert(0, new ListItem("Root", "0"));
			dropIndex.Items.Clear();
			MiscUtility.FillIndex(dropIndex, 30, 1);

			TabInfo tab = TabController.GetTab(curTabId);
			if (tab != null)
			{
				txtID.Text = tab.TabID.ToString();
				dropPath.SelectedValue = tab.TabLayout;
				txtName.Text = tab.TabName;
				txtUrl.Text = tab.TabUrl;
				txtTitle.Text = tab.TabTitle;
				txtDescription.Text = tab.TabDescription;
				MiscUtility.SelectItemFromList(dropParent, tab.TabParentID.ToString());
				MiscUtility.SelectItemFromList(dropIndex, tab.TabOrder.ToString());
				chkEnable.Checked = tab.TabEnable;
				chkVisble.Checked = tab.TabVisible;
			}
		}
        private void LoadForCurNode(TreeNode curNode, DataTable source)
		{
			int curTabId = ConvertUtility.ToInt32(curNode.Value);
			DataRow[] drChilds = source.Select("TabParentID = " + curTabId);
			foreach (DataRow row in drChilds)
			{
                TreeNode node = new TreeNode();
				node.Text = row["TabName"].ToString();
				node.Value = row["TabID"].ToString();
                node.Expanded = false;
				curNode.ChildNodes.Add(node);
				LoadForCurNode(node, source);
			}
		}
        protected void btnUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				int tabId = ConvertUtility.ToInt32(txtID.Text);
				if (tabId == 0) return;

				TabInfo tab = TabController.GetTab(tabId);
				if (tab == null) return;

				tab.TabDescription = txtDescription.Text.Trim();
				tab.TabEnable = chkEnable.Checked;
                tab.TabLayout = dropPath.SelectedValue.Trim();
				tab.TabName = txtName.Text.Trim();
				tab.TabOrder = ConvertUtility.ToInt32(dropIndex.SelectedValue);
				tab.TabParentID = ConvertUtility.ToInt32(dropParent.SelectedValue);
				tab.TabTitle = txtTitle.Text.Trim();
				tab.TabUrl = txtUrl.Text.Trim();
				tab.TabVisible = chkVisble.Checked;
                tab.PortalID = ConvertUtility.ToInt32(AppEnv.PortalId());

				TabController.UpdateTab(tab);				
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
		}
        protected void btnAddNew_Click(object sender, EventArgs e)
		{
			try
			{
				TabInfo tab = new TabInfo();
				tab.TabDescription = txtDescription.Text.Trim();
				tab.TabEnable = chkEnable.Checked;
                tab.TabLayout = dropPath.SelectedValue.Trim();
				tab.TabName = txtName.Text.Trim();
				tab.TabOrder = ConvertUtility.ToInt32(dropIndex.SelectedValue);
				tab.TabParentID = ConvertUtility.ToInt32(dropParent.SelectedValue);
				tab.TabTitle = txtTitle.Text.Trim();
				tab.TabUrl = txtUrl.Text.Trim();
				tab.TabVisible = chkVisble.Checked;
				tab.TabLang = AppEnv.GetLanguage();
                tab.PortalID = ConvertUtility.ToInt32(AppEnv.PortalId());

				TabController.AddTab(tab);
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
		}
        protected void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				int tabId = ConvertUtility.ToInt32(txtID.Text);
				TabController.DeleteTab(tabId);			
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
		}
        protected void btnEmpty_Click(object sender, EventArgs e)
		{
			txtID.Text = string.Empty;
            dropPath.SelectedIndex = -1;
			txtName.Text = string.Empty;
			txtUrl.Text = string.Empty;
			txtTitle.Text = string.Empty;
			txtDescription.Text = string.Empty;
			chkEnable.Checked = false;
			chkVisble.Checked = false;
		}
		
        protected void tvwCmds_SelectedNodeChanged(object sender, EventArgs e)
        {            
            txtID.Text = tvwCmds.SelectedNode.Value;
            lblUpdateStatus.Text = string.Empty;
        }		
	}
}